﻿using Core.Configuration.Api;
using Core.Configuration.Config;
using IconYuan.Api.Services;
using IconYuan.Manager.Orders;
using IconYuan.Manager.Products;
using IconYuan.Manager.Stores;
using IconYuan.Manager.Users;
using IconYuan.Model.DB;
using IconYuan.Model.DTO.Orders;
using IconYuan.Model.Entity.Orders;
using IconYuan.Model.Entity.Stores;
using IconYuan.Model.Entity.Users;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace IconYuan.Api.Controllers.Admins.Orders
{
    public class OrderController : AdminApi
    {
        readonly OrderManager orderSvc;
        readonly OrderGoodsManager orderGoodsSvc;
        readonly OrderTraceManager orderTraceSvc;
        readonly ShopManager shopSvc;
        readonly SkuManager skuSvc;
        readonly UserManager userSvc;
        public OrderController()
        {
            orderSvc = new OrderManager();
            orderGoodsSvc = new OrderGoodsManager();
            orderTraceSvc = new OrderTraceManager();
            shopSvc = new ShopManager();
            skuSvc = new SkuManager();
            userSvc = new UserManager();
        }

        
        [HttpPost]
        public async Task<PageList<OrderDto>> List(AdminOrderQuertDto model)
        {
            
            var expGoods = Expressionable.Create<OrderGoods>();
            var expShop = Expressionable.Create<Shop>();
            var exp = Expressionable.Create<Order>();
            exp.And(x => x.jump == model.jump);
            exp.AndIF(model.is_settled > -1,x => x.is_settled == model.is_settled);
            exp.AndIF(model.user_id > 0, x => x.user_id == model.user_id);
            if (!string.IsNullOrEmpty(model.mobile))
            {
               var userList= await userSvc.GetListAsync(x => x.mobile != "" && x.mobile.Contains(model.mobile));
                var uids = new List<int>() { -1 };
                uids.AddRange(userList.Select(x => x.user_id));
                exp.And(x => uids.Contains(x.user_id));
            }

            //exp.And(a => SqlFunc.Subqueryable<User>().Where(b => b.mobile.Contains(model.mobile) && b.user_id == a.user_id));
            if (model.status > 0)
                exp.And(x => x.order_status == model.status);
            else
            {
                var statusList = new List<int>() { 30, 70 };
                exp.And(x => statusList.Contains(x.order_status));
            }

            var result = await orderSvc.GetPageListAsync(exp.ToExpression(), y => y.order_id, OrderByType.Desc, model.page);
            result.data.ForEach(x =>
            {
                expGoods.Or(y => y.order_id == x.order_id);
                expShop.Or(y => y.shop_id == x.shop_id);
            });

            //var shopList = await shopSvc.GetListAsync(expShop.ToExpression());
            var shopList = new List<Shop>();
            var goodsList = await orderGoodsSvc.GetListAsync(expGoods.ToExpression(), y => y.order_id, OrderByType.Desc);
            foreach (var goods in goodsList)
            {
                goods.goods_image = $"{GlobalConfig.CdnDomain}{goods.goods_image}";
            }

            var list = result.data.Select(x => OrdersServices.ToOrderDto(x, goodsList, shopList)).ToList();
            return new PageList<OrderDto>()
            {
                data = list,
                current_page = result.current_page,
                last_page = result.last_page,
                per_page = result.per_page,
                total = result.total,
            };
        }


        /// <summary>
        /// 订单分佣
        /// </summary>
        /// <param name="order_id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<object> Profit(int order_id)
        {
            var order = await orderSvc.GetAsync(order_id);
            return await PayServices.Profit(order);
        }
    }
}
